
new Vue({
    el: '#app',
    delimiters:['[[', ']]'],

    data: {
        gifts: [],
    },

    methods: {
        /**
         * 请求礼物列表
         */
        requestGiftList() {
            let url = "{% url 'gma:api_get_gift_list' %}";
            new my_ajax(url, {}, '', '', (data) => {
                console.table(data);
                this.gifts = data;

                this.gifts.forEach(gift => {
                    gift.edit_url = `/gma/config/gift/${gift.id}/edit/`;
                });
            });
        },

        /**
         * 点击礼物图标
         * @param gift
         */
        clickGift(gift) {
            if (gift.enabled === 1) {
                this.disableGift(gift.id);
            } else {
                this.enableGift(gift.id);
            }
        },

        /**
         * 启用礼物
         *
         * @param gift_id
         */
        enableGift(gift_id) {
            let data = {
                'gift_id': gift_id,
            };

            let url = "{% url 'gma:api_enable_gift' %}";
            new my_ajax(url, data, '', '', () => {
                show_toast('成功开启');
                this.requestGiftList();
            });
        },

        /**
         * 禁用礼物
         * @param gift_id
         */
        disableGift(gift_id) {
            let data = {
                'gift_id': gift_id,
            };

            let url = "{% url 'gma:api_disable_gift' %}";
            new my_ajax(url, data, '', '', () => {
                show_toast('成功关闭');
                this.requestGiftList();
            });
        },
    },

    mounted() {
        this.requestGiftList();
    },
});
